package com.imrookie.gendoc.db.service.impl;

import com.imrookie.gendoc.db.model.Column;
import com.imrookie.gendoc.db.model.Index;
import com.imrookie.gendoc.db.model.IndexWrap;
import com.imrookie.gendoc.db.model.Table;
import com.imrookie.gendoc.db.service.DBService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author wangjiyu@imdada.cn 2019/8/9
 */
@Service
public class DBServiceImpl implements DBService {

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    /**
     * 查询表信息
     *
     * @return
     */
    @Override
    public List<Table> getTableList() {
        return sqlSessionTemplate.selectList("com.imrookie.gendoc.db.getTableListOfSimpleInfo");
    }

    /**
     * 查询表的字段信息
     *
     * @param tableName 表名
     * @return
     */
    @Override
    public List<Column> getColumsListByTableName(String tableName) {
        return sqlSessionTemplate.selectList("com.imrookie.gendoc.db.getColumsListByTableName", tableName);
    }

    /**
     * 查询表索引
     *
     * @param tableName 表名
     * @return
     */
    @Override
    public List<Index> getIndexListByTableName(String tableName) {
        return sqlSessionTemplate.selectList("com.imrookie.gendoc.db.getIndexListByTableName", tableName);
    }

    /**
     * 查询表索引包装信息
     *
     * @param tableName 表名
     * @return
     */
    @Override
    public List<IndexWrap> getIndexWrapListByTableName(String tableName) {
        //查询索引
        List<Index> list = this.getIndexListByTableName(tableName);
        if (list == null || list.isEmpty()) {
            return null;
        }
        Map<String, IndexWrap> map = new LinkedHashMap<String, IndexWrap>();
        for (Index index : list) {
            if (map.containsKey(index.getKey_name())) {
                IndexWrap indexWrap = map.get(index.getKey_name());
                indexWrap.setColumns(indexWrap.getColumns() + "," + index.getColumn_name());
                if ("PRIMARY".equals(index.getKey_name())) {
                    indexWrap.setCategory("联合主键");
                } else if (new Integer(0).equals(index.getNon_unique())) {
                    indexWrap.setCategory("联合唯一索引");
                } else {
                    indexWrap.setCategory("联合索引");
                }
            } else {
                IndexWrap indexWrap = new IndexWrap();
                indexWrap.setIndexName(index.getKey_name());
                indexWrap.setColumns(index.getColumn_name());
                if ("PRIMARY".equals(index.getKey_name())) {
                    indexWrap.setCategory("主键索引");
                } else if (new Integer(0).equals(index.getNon_unique())) {
                    indexWrap.setCategory("唯一索引");
                } else if ("FULLTEXT".equals(index.getIndex_type())) {
                    indexWrap.setCategory("全文索引");
                } else {
                    indexWrap.setCategory("普通索引");
                }
                map.put(index.getKey_name(), indexWrap);
            }
        }
        List<IndexWrap> list1 = new ArrayList<IndexWrap>(map.size());
        for (Map.Entry<String, IndexWrap> entry : map.entrySet()) {
            list1.add(entry.getValue());
        }
        return list1;
    }
}
